To run this example you need to install pymongo.

pip install pymongo


In [ ]:
import pymongo

In [3]:
client = pymongo.MongoClient('localhost', 27017)
db = client['test-database']
db


Out[3]:
Database(MongoClient(host=['localhost:27017'], document_class=dict, tz_aware=False, connect=True), 'test-database')

In [4]:
collection = db['test-collection']
collection


Out[4]:
Collection(Database(MongoClient(host=['localhost:27017'], document_class=dict, tz_aware=False, connect=True), 'test-database'), 'test-collection')

In [5]:
collection.estimated_document_count()


Out[5]:
0

In [6]:
test_data = {'age': 29, 'gender': 'F', 'occupation': 'Software Engineer'}
test_data


Out[6]:
{'age': 29, 'gender': 'F', 'occupation': 'Software Engineer'}

In [7]:
type(test_data)


Out[7]:
dict

In [8]:
insert_result = collection.insert_one(test_data)
insert_result


Out[8]:
<pymongo.results.InsertOneResult at 0x22c3c9bc7c8>

In [9]:
insert_result.acknowledged


Out[9]:
True

In [10]:
insert_result.inserted_id


Out[10]:
ObjectId('5c4746aa0adc743644504254')

In [11]:
type(insert_result.inserted_id)


Out[11]:
bson.objectid.ObjectId

In [12]:
collection.estimated_document_count()


Out[12]:
1

In [13]:
delete_result = collection.delete_one({'age': 29})
delete_result


Out[13]:
<pymongo.results.DeleteResult at 0x22c3d21d488>

In [14]:
delete_result.acknowledged


Out[14]:
True

In [15]:
delete_result.deleted_count


Out[15]:
1

In [16]:
client.close()

In [ ]: